/*
 * SearchEngine.java
 *
 * Created on 6 March 2006, 14:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mac5855.app.lucene.search;

import java.io.IOException;

import org.apache.lucene.analysis.br.BrazilianAnalyzer;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Weight;

/**
 * 
 * @author John
 */
public class SearchEngine {
	private IndexSearcher searcher = null;
	private QueryParser parser = null;
	private Weight weight = null;

	public Weight getWeight() {
		return weight;
	}

	/** Creates a new instance of SearchEngine */
	public SearchEngine() throws IOException {
		try {
			Indexer indexer = new Indexer();
			indexer.rebuildIndexes();
			searcher = new IndexSearcher("index-directory");			
		} catch (IOException e) {
			Indexer indexer = new Indexer();
			indexer.rebuildIndexes();
			searcher = new IndexSearcher("index-directory");
		}
		parser = new QueryParser("content", new BrazilianAnalyzer());
	}

	public Hits performSearch(String queryString) throws IOException, ParseException {
		
		
		System.out.println(queryString);
		String querySemAcento = Analisador.remover(queryString);
		System.out.println(querySemAcento);
		Query query = parser.parse(querySemAcento);
		weight = query.weight(searcher);
		Hits hits = searcher.search(query);
		// Hits hits = searcher.search(query,filter);
		return hits;
	}

	public IndexSearcher getSearcher() {
		return searcher;
	}

}
